package com.keta.foundation.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.keta.foundation.entity.Module;
import com.keta.foundation.entity.Role;
import com.keta.foundation.entity.RolePermission;
import com.keta.foundation.log.Log;
import com.keta.foundation.log.LogUtil;
import com.keta.foundation.service.ModuleService;
import com.keta.foundation.service.RolePermissionService;
import com.keta.foundation.service.RoleService;
import com.keta.foundation.util.dwz.AjaxObject;
import com.keta.foundation.util.dwz.Page;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

	private static final String ROLE_LIST = "role/list";
	private static final String ROLE_VIEW = "role/view";
	private static final String ROLE_CREATE = "role/create";
	private static final String ROLE_EDIT = "role/edit";

	@Autowired
	private RoleService roleService;

	@Autowired
	private ModuleService moduleService;
	
	@RequiresPermissions("Role:view")
	@RequestMapping(value = "/list", method = { RequestMethod.POST, RequestMethod.GET })
	public String list(Model model) {

		Page page = new Page();

		List<Role> listRole = roleService.getAll(page);

		model.addAttribute("listRole", listRole);
		model.addAttribute("page", page);

		return ROLE_LIST;
	}

	@RequiresPermissions("Role:view")
	@RequestMapping(value = "/view/{id}", method = { RequestMethod.GET, RequestMethod.POST })
	public String view(@PathVariable Long id, Model model) {

		Role role = roleService.get(id);

		Module rootModule = moduleService.getByParentId(null).get(0);

		model.addAttribute("role", role);
		model.addAttribute("rootModule", rootModule);
		return ROLE_VIEW;
	}

	@RequiresPermissions("Role:create")
	@RequestMapping(value = "/create", method = { RequestMethod.GET })
	public String preCreate(Model model) {

		Module rootModule = moduleService.getByParentId(null).get(0);

		model.addAttribute("rootModule", rootModule);

		return ROLE_CREATE;
	}

	@Log(message = "创建了角色:{0}")
	@RequiresPermissions("Role:create")
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public @ResponseBody String create(@Valid Role role, Model model) {

		List<RolePermission> rolePermissions = new ArrayList<RolePermission>();
		// 删除treeTable js添加的input提交的空数据
		for (RolePermission rolePermission : role.getRolePermissions()) {
			if (rolePermission.getPermission() != null && rolePermission.getPermission().getId() != null) {
				rolePermissions.add(rolePermission);
			}
		}

		role.setRolePermissions(rolePermissions);

		roleService.saveOrUpdate(role);
		LogUtil.putArgs(new Object[] { role.getName() });
		return AjaxObject.newOk("添加角色成功！").toString();
	}

	@RequiresPermissions("Role:edit")
	@RequestMapping(value = "/edit/{id}", method = { RequestMethod.GET })
	public String preEdit(@PathVariable Long id, Model model) {

		Role role = roleService.get(id);

		Module rootModule = moduleService.getByParentId(null).get(0);

		model.addAttribute("role", role);
		model.addAttribute("rootModule", rootModule);

		return ROLE_EDIT;
	}

	
	@Log(message = "修改了角色:{0}")
	@RequiresPermissions("Role:edit")
	@RequestMapping(value = "/edit", method = { RequestMethod.POST })
	public @ResponseBody String edit(@Valid Role role, Model model) {

		roleService.saveOrUpdate(role);
		LogUtil.putArgs(new Object[] { role.getName() });
		return AjaxObject.newOk("编辑角色成功！").toString();
	}

	@Log(message = "删除了角色,角色Id为:{0}")
	@RequiresPermissions("Role:delete")
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(Long[] ids) {

		roleService.delete(ids);

		LogUtil.putArgs(new Object[] { Arrays.toString(ids) });
		return AjaxObject.newOk("角色删除成功！").setCallbackType("").toString();
	}

}
